import { createStore } from 'vuex'
import { getinfo } from '~/api/manager'
import { removeToken } from "~/composables/auth";


// 创建一个新的 store 实例
const store = createStore({
  state () {
    return {
     //用户信息
     user:{},
     //侧边宽度
     asideWidth:"250px",
     //侧边菜单数组
     menus:[],
     //权限相关数组
     ruleNames:[]
    }
  },
  mutations: {
    //记录用户信息
    SET_USERINFO(state,user){
        state.user=user
    },
    //展开/缩起侧边
    handleAsideWidth(state){
      state.asideWidth=state.asideWidth=='250px'?'64px':'250px'
    },
    //侧边数组
    SET_MENUS(state,menus){
      state.menus=menus
    },
    //权限相关
    SET_RULENAMES(state,ruleNames){
      state.ruleNames=ruleNames
    }
  },
  actions:{
    //获取当前登录信息
    getinfo({commit}){
     return new Promise((resolve,reject)=>{
      getinfo().then(res=>{
        commit('SET_USERINFO',res)
        commit('SET_MENUS',res.menus)
        commit('SET_RULENAMES',res.ruleNames)

        resolve(res)
      }).catch(err=>reject(err))
     })
    },
    //退出登录
    logout({commit}){
      removeToken()
      commit('SET_USERINFO',{})
    }
  }
})

export default store